package com.cloudcanal.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.reflections.Reflections;

import com.clougence.cloudcanal.sdk.api.modelv2.SchemaInfo;

import cn.hutool.core.util.ReflectUtil;

public class RecordModifyConvertFactory {
	
	public static Map<String , RecordModifyConvert> recordModifyConvertMap = new HashMap<String, RecordModifyConvert>();
	
	static {
		List<RecordModifyConvert> converts = getRecordModifyConvertInstances(RecordModifyConvertFactory.class.getPackage().getName());
		converts.forEach(it-> recordModifyConvertMap.put(it.tableName(), it));
	}
	public static RecordModifyConvert get(SchemaInfo schemaInfo) {
		return recordModifyConvertMap.get(schemaInfo.getTable());
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private static List<RecordModifyConvert> getRecordModifyConvertInstances(String packge){
		List<RecordModifyConvert> converts = new ArrayList<RecordModifyConvert>();
		Reflections reflections = new Reflections(packge);
		Set set = reflections.getSubTypesOf(DataChangeModify.class);
		set.forEach(it->{
			Class<?> class1 = (Class<?>) it;
			DataChangeModify convert = (DataChangeModify) ReflectUtil.newInstance(class1);
			converts.add(convert);
		});
		return converts;
	}
	
	public static void main(String[] args) {
		System.out.println(RecordModifyConvertFactory.recordModifyConvertMap.size());
	}
}
